import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { DateOptionService } from 'app/share/date-option.service';
import { FormatDate } from 'app/classes/Device';
import { IMyDateModel, IMyDpOptions } from 'mydatepicker';

@Component({
  selector: 'app-time-type',
  templateUrl: './time-type.component.html',
  styleUrls: ['./time-type.component.css']
})
export class TimeTypeComponent implements OnInit {
  @Input()
  begindate: string;
  @Input()
  enddate: string;
  @Input()
  hasEnd = true;

  @Output()
  emitDate = new EventEmitter<EmitData>();

  dateType = 'day';
  placeholder = '请选择日';
  public myDatePickerOptions: IMyDpOptions;
  constructor(
    public dateOption: DateOptionService,
  ) { }

  ngOnInit() {
    // 初始化日期选择器的配置项
    this.myDatePickerOptions = this.dateOption.getOptions();
  }

  changeDate(dateType) {
    this.begindate = null;
    this.enddate = null;
    switch ( dateType ) {
      case 'month': {
        this.placeholder = '请选择月';
      }; break;
      case 'year': {
        this.placeholder = '请选择年';
      }; break;
      default : {
        this.placeholder = '请选择日';
      }; break;
    }
    this.myDatePickerOptions = this.dateOption.setOption(dateType);
    this.dateType = dateType;
  }
  start(event: IMyDateModel) {
    const emitData = new EmitData();
    emitData.datetype = this.dateType;
    emitData.event = event;
    emitData.type = 'start';
    this.emitDate.emit(emitData);
  }
  end(event: IMyDateModel) {
    const emitData = new EmitData();
    emitData.datetype = this.dateType;
    emitData.event = event;
    emitData.type = 'end';
    this.emitDate.emit(emitData);
  }

}
export class EmitData {
  event: IMyDateModel;
  type: string;
  datetype: string;
}

